package network;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JProgressBar;

public class FileClient extends JFrame implements Runnable {

	private JPanel contentPane;
	private JTextField result;
	private JButton btnNewButton;
	private JButton btnNewButton_1;

	long filesize;
	Socket socket;
	String fileName;

	private JTextField serverIP;
	private JLabel lblIp;
	private JLabel status;
	private JProgressBar progressBar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileClient frame = new FileClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FileClient() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 188);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getResult());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getServerIP());
		contentPane.add(getLblIp());
		contentPane.add(getStatus());
		contentPane.add(getProgressBar());
	}

	public void openFile() {
		JFileChooser fc = new JFileChooser(); // 파일 찾는 코드

		int yn = fc.showOpenDialog(null);
		if (yn == JFileChooser.APPROVE_OPTION) {
			result.setText(fc.getSelectedFile().toString());
			filesize = fc.getSelectedFile().length();                      //

			progressBar.setMaximum((int) filesize);                        //

		}
	}

	public void run() {
		try {
			socket = new Socket(serverIP.getText(), 4444);
			FileInputStream fis = new FileInputStream(
					new File(result.getText()));

			DataOutputStream dos = new DataOutputStream(
					socket.getOutputStream());
			byte[] d = new byte[102400];
			int len = 0;
			int totlen = 0;                             //

			dos.writeUTF(result.getText());

			while ((len = fis.read(d)) != -1) {
				dos.write(d, 0, len);
				totlen += len;                           //
				progressBar.setValue(totlen);            //
				Thread.sleep(100);                       //
			}
			dos.close();
			fis.close();
			socket.close();

		} catch (Exception ex) {

		}

	}

	private JTextField getResult() {
		if (result == null) {
			result = new JTextField();
			result.setBounds(12, 10, 312, 21);
			result.setColumns(10);
		}
		return result;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("찾아라!!");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					openFile();

				}
			});
			btnNewButton.setBounds(325, 9, 97, 23);
		}
		return btnNewButton;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("전송");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {

						Thread th = new Thread(FileClient.this);
						th.start();

					} catch (Exception ex) {

					}
				}

			});
			btnNewButton_1.setBounds(215, 42, 207, 23);
		}
		return btnNewButton_1;
	}

	private JTextField getServerIP() {
		if (serverIP == null) {
			serverIP = new JTextField();
			serverIP.setHorizontalAlignment(SwingConstants.LEFT);
			serverIP.setText("192.168.10.1");
			serverIP.setBounds(91, 43, 116, 21);
			serverIP.setColumns(10);
		}
		return serverIP;
	}

	private JLabel getLblIp() {
		if (lblIp == null) {
			lblIp = new JLabel("IP");
			lblIp.setHorizontalAlignment(SwingConstants.RIGHT);
			lblIp.setBounds(22, 46, 57, 15);
		}
		return lblIp;
	}

	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("파일선택하쇼~!");
			status.setBounds(12, 125, 410, 15);
		}
		return status;
	}

	private JProgressBar getProgressBar() {
		if (progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setBounds(12, 101, 410, 14);
		}
		return progressBar;
	}
}
